function registerSW() {
  if (!window.navigator.serviceWorker) {
    console.warn('Service Worker is not supported');
    return
  }
  // 注册Service Worker，scope 控制serviceWorker的生效范围
  window.navigator.serviceWorker.register('./sw.js', { scope: './' })
  .then((registration) => {
    console.log('service worker registered', registration)
  })
  .catch((err) => {
    console.warn('service worker registration failed', err)
  })
}

// 等待页面加载完成，再注册Service Worker
window.addEventListener('load', registerSW)